package com.colin.app.activity.other.serializable;

import android.os.Parcel;
import android.os.Parcelable;

import com.colin.app.R;
import com.colin.app.activity.other.OtherContract;
import com.colin.app.activity.other.OtherPresenterImpl;
import com.colin.app.app.AppActivity;
import com.colin.app.bean.DemoBean;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class SerializableActivity extends AppActivity implements OtherContract.OtherView {

    private OtherContract.OtherPresenter mOtherPresenter = null;

    @Override
    protected void onDestroy() {
        if (null != mOtherPresenter) {
            mOtherPresenter.destroy();
            mOtherPresenter = null;
        }
        super.onDestroy();
    }

    @Override
    public void initPresenter() {
        DemoBean demoBean = getIntent().getExtras().getParcelable("clazz");
        mOtherPresenter = new OtherPresenterImpl(this, this, demoBean);
    }

    @Override
    public int initViewLayoutID() {
        return R.layout.activity_serializable;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {
        mOtherPresenter.setData();
    }

    /**
     * 序列化过程
     */
    private void test1() {
        try {
            SerializableBean serializableBean = new SerializableBean(1, "Colin");
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("cache.text"));
            objectOutputStream.writeObject(serializableBean);
            objectOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 反序列化过程
     */
    private void test2() {
        try {
            ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("cache.text"));
            SerializableBean serializableBean = (SerializableBean) objectInputStream.readObject();
            objectInputStream.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Created by Colin on 2017/2/25.
     * Java序列化类
     */

    private class SerializableBean implements Serializable {
        //        serialVersionUID反序列化
        private int id;
        private String name;

        public SerializableBean() {
        }

        public SerializableBean(int id, String name) {
            this.id = id;
            this.name = name;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return "SerializableBean{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    '}';
        }

    }

    /**
     * 课程类 实现Android序列化
     */
    private class CourseParcelable implements Parcelable {
        private int id;
        private String name;
        private float score;

        public CourseParcelable() {
        }

        public CourseParcelable(int id, String name, float score) {
            this.id = id;
            this.name = name;
            this.score = score;
        }

        protected CourseParcelable(Parcel in) {
            id = in.readInt();
            name = in.readString();
            score = in.readFloat();
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeInt(id);
            dest.writeString(name);
            dest.writeFloat(score);
        }

        @Override
        public int describeContents() {
            return 0;
        }

        public final Creator<CourseParcelable> CREATOR = new Creator<CourseParcelable>() {
            @Override
            public CourseParcelable createFromParcel(Parcel in) {
                return new CourseParcelable(in);
            }

            @Override
            public CourseParcelable[] newArray(int size) {
                return new CourseParcelable[size];
            }
        };

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public float getScore() {
            return score;
        }

        public void setScore(float score) {
            this.score = score;
        }

        @Override
        public String toString() {
            return "CourseParcelable{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", score=" + score +
                    ", CREATOR=" + CREATOR +
                    '}';
        }
    }

    /**
     * 学生类 实现序列化
     */
    private class StudentParcelable implements Parcelable {
        private int id;
        private String name;
        private CourseParcelable courseParcelable;

        public StudentParcelable() {
        }

        public StudentParcelable(int id, String name, CourseParcelable course) {
            this.id = id;
            this.name = name;
            this.courseParcelable = course;
        }

        protected StudentParcelable(Parcel in) {
            id = in.readInt();
            name = in.readString();
            courseParcelable = in.readParcelable(CourseParcelable.class.getClassLoader());
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeInt(id);
            dest.writeString(name);
            dest.writeParcelable(courseParcelable, flags);
        }

        @Override
        public int describeContents() {
            return 0;
        }

        public final Creator<StudentParcelable> CREATOR = new Creator<StudentParcelable>() {
            @Override
            public StudentParcelable createFromParcel(Parcel in) {
                return new StudentParcelable(in);
            }

            @Override
            public StudentParcelable[] newArray(int size) {
                return new StudentParcelable[size];
            }
        };

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public CourseParcelable getCourseParcelable() {
            return courseParcelable;
        }

        public void setCourseParcelable(CourseParcelable courseParcelable) {
            this.courseParcelable = courseParcelable;
        }

        @Override
        public String toString() {
            return "StudentParcelable{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", courseParcelable=" + courseParcelable +
                    ", CREATOR=" + CREATOR +
                    '}';
        }
    }
}
